package cn.service;

import cn.util.HttpSend;
import cn.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TemplateMessageService {

    public static void main(String[] args) {
        String appId = "wx28f50c8a08e5468b";
        String appSecret = "27b2530f39b240763642405e4a033cdb";
        String accessToken = "";
        String openId = "oJHW86VLEvNe58Seo5pGyTEiJMfA";
        String s = HttpSend.sendGet(appId, appSecret);
        JSONObject jsonObject = JSONObject.parseObject(s);
        accessToken = jsonObject.getString("access_token"); // 获取到了access_token，调用接口都要用到的，有时效
        sendTemp(accessToken,openId);
    }

    /**
     *
     * @param accessToken  访问令牌
     * @param openId  你要发送给某个用户的openid
     */
    public static void sendTemp(String accessToken, String openId) {
        // 封装要发送的json
        Map<String, Object> map = new HashMap();
        map.put("touser", openId);//你要发送给某个用户的openid 前提是已关注该公众号,该openid是对应该公众号的，不是普通的openid
        map.put("template_id", "J-MK2vinPWTqqjiCkXIQrWVzk16PHH3KyfyztSN7oAU");//模板消息id
        Map firstMap = new HashMap();
        firstMap.put("value", "新的消费通知！"); //内容
        firstMap.put("color", "#173177"); //字体颜色
        Map remarkMap = new HashMap();
        remarkMap.put("value", "宝，你裤衩子露出来了！");
        remarkMap.put("color", "#fff");
        Map dataMap = new HashMap();
        dataMap.put("first", firstMap);
        dataMap.put("remark", remarkMap);
        map.put("data", dataMap);
        String urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
        String r = HttpUtil.getJsonData(JSONObject.parseObject(JSON.toJSONString(map)), urls + accessToken); //发送模板消息，这里有个工具类，我给你哟
        System.out.println("-->" + r);
    }

    public static void sendTempPoint(String appid, String accessToken, String appSecret, Integer Id, String name, String mobile, Double amount, String wechatOpenId) {
        String accTemp = HttpSend.sendGet(appid,appSecret);
        JSONObject jsonObject = JSONObject.parseObject(accTemp);
        System.out.println("1--->" + jsonObject);
        accessToken = jsonObject.getString("access_token"); // 获取到了access_token，调用接口都要用到的，有时效
        // 封装要发送的json
        Map<String, Object> map = new HashMap();
        map.put("touser", wechatOpenId);//你要发送给某个用户的openid 前提是已关注该公众号,该openid是对应该公众号的，不是普通的openid
        map.put("template_id", "672dPs0Jwl9zLvhVrncI0nyEHxX3Mx-fPNLBcIEb05A");//模板消息id
        //map.put("url","https://www.vipkes.cn");//用户点击模板消息，要跳转的地址
        // 封装miniprogram 跳转小程序用,不跳不要填
        Map<String, String> mapA = new HashMap<>();
        mapA.put("appid", ""); //小程序appid
        mapA.put("pagepath", ""); //小程序页面pagepath
        map.put("miniprogram", mapA);

        // 以下就是根据模板消息的格式封装好，我模板的是：问题反馈结果通知  可以和我一样试试
        // 封装first
        Map firstMap = new HashMap();
        firstMap.put("value", "会员积分消费提醒！"); //内容
        firstMap.put("color", "#173177"); //字体颜色
        String newname;
        try {
            newname = (java.net.URLDecoder.decode(name, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            newname = name;
        }

        // 封装keyword1 提交的问题
        Map XM = new HashMap();
        XM.put("value", newname);
        XM.put("color", "#fff");

        // 封装keyword2此处也可以是商品名
        Map KH = new HashMap();
        KH.put("value", Id);
        KH.put("color", "#fff");
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 封装keyword3此处可以是商品价格
        Map CONTENTS = new HashMap();
        CONTENTS.put("value", "尊敬的用户,您的账户于" + simpleDateFormat.format(date) + "增加了" + amount + "积分");
        CONTENTS.put("color", "#fff");


        Map remarkMap = new HashMap();
        remarkMap.put("value", "消息惠顾");
        remarkMap.put("color", "#fff");

        // 封装data
        Map dataMap = new HashMap();
        dataMap.put("first", firstMap);
        dataMap.put("XM", XM);
        dataMap.put("KH", KH);
        dataMap.put("CONTENTS", CONTENTS);
        dataMap.put("remark", remarkMap);

        map.put("data", dataMap);
        String r = HttpUtil.getJsonData(JSONObject.parseObject(JSON.toJSONString(map)), "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken); //发送模板消息，这里有个工具类，我给你哟
        System.out.println("-->" + r);
    }

}
